import sys
from ai.upload_file import upload_file
from ai.chat_completion import stream_chat_completion

def image_to_text_ai(image_path):
    
    try:
        # 上传文件获取ID
        file = upload_file(image_path)
        if not file:
            print("文件上传失败")
            sys.exit(1)

        # 调用OCR接口
        result = stream_chat_completion(file.get('id'))
        
        if result and result.get('choices') and result['choices'][0]['message'].get('content'):
            print("\nAI提取的文字内容：")
            print("-" * 20)
            print(result['choices'][0]['message']['content'])
            print("-" * 20)
            sys.exit(0)
        else:
            print("未能提取任何文字内容")
            sys.exit(1)
            
    except Exception as e:
        print(f"处理过程中发生错误: {str(e)}")
        sys.exit(1)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("请提供图片路径作为参数")
        sys.exit(1)
    image_path = sys.argv[1]
    image_to_text_ai(image_path)